package com.redside.webapp.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Retrieves information from wems.properties file.
 * 
 * @author BGEARRIN
 *
 */
public class ApplicationProperties {

    private static Properties props;
    private static final Log logger = LogFactory.getLog(ApplicationProperties.class);

    private ApplicationProperties(){
    }

	/**
	 * initial method for this class that loads the properties file.
	 */
	private synchronized static void init() throws IOException {

		if (null == props) {
			logger.debug("Loading properties file...");

			try {
				InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("wems.properties");
				props = new Properties();
				props.load(stream);
				stream.close();
			}
			catch (IOException e) {				
				logger.error("Error reading wems properties file" + e);
				throw new IOException();
			}
		}
	}


	/**
	 * method that returns the value for a property requested
     * @param property
     * @return String 
	 */
	public static String getProperty(final String property) {
        if (null == props) {
			try {
				init();
			} catch (IOException e) {
				logger.error("Error reading wems properties file" + e);
			}
		}

        return props.getProperty(property);		
	}

}